
#include "client.h" 
CClient::CClient(const string& strAddr, int iPort)
{
    WSADATA   wsaData;
    int err = WSAStartup( MAKEWORD(2, 2), &wsaData ); 
    if (err == SOCKET_ERROR)
        cout << "WSAStartup error" << endl; 
        m_sockfd = socket(AF_INET, SOCK_STREAM, 0); /*需要错误检查 */
    if (m_sockfd == -1)
    {
        cout << "socket fail!" << endl;
        return;
    }
    cout << "socket success!socket:" << m_sockfd << endl; 
    memset(&m_addr, 0x00, sizeof(m_addr)); 
    m_addr.sin_family = AF_INET; /* host byte order */ 
    m_addr.sin_port = htons(iPort); /* short, network byte order */ 
    m_addr.sin_addr.s_addr = inet_addr(strAddr.c_str());
}

CClient::~CClient()
{
    WSACleanup(); 
}

BOOL CClient::Connect()
{
    int iRet = connect(m_sockfd, (struct sockaddr *)&m_addr, sizeof(m_addr));
    if (iRet == -1) 
    {
        cout << "connect fail!" << endl; 
        return FALSE;
    }
    return TRUE; 
}

BOOL CClient::Send(const string& strSend)
{    
    int iRet = send(m_sockfd, strSend.c_str(), strSend.length(), 0);
    if (iRet == -1)
    {
        cout << "connect fail!" << endl; 
        return FALSE;
    }
    cout << "Send is OK!";
    return TRUE; 
} 
